#include "sgl_radiobtn.h"
#include "stdio.h"

int sgl_radiobtn_init(sgl_radiobtn_t *radiobtn, sgl_obj_t* parent, bool status)
{
    sgl_obj_t* obj;
    if(parent == NULL) {
        return -1;
    }
    radiobtn->text = NULL;
    radiobtn->font = NULL;
    obj = &radiobtn->obj;
    obj->parent = parent;
    obj->event_data = NULL;
    obj->ev_stat = SGL_EVENT_NORMAL;
    obj->obj_type = SGL_OBJ_RADIOBUTTON;
    sgl_obj_add_src_act_list(obj);
    sgl_obj_add_dirty_list(obj);
    return 0;    
}

sgl_obj_t* sgl_radiobtn_create(sgl_obj_t* parent, bool status)
{
    int ret;
    sgl_radiobtn_t *radiobtn;
    if(parent == NULL) {
        return NULL;
    }
    radiobtn = (sgl_radiobtn_t *)sgl_alloc(sizeof(sgl_radiobtn_t));
    if(radiobtn == NULL)
        return NULL;
    ret = sgl_radiobtn_init(radiobtn, parent, status);
    if(ret) {
        return NULL;
    }
    return  &radiobtn->obj;
}

void sgl_radiobtn_set_status(sgl_obj_t *obj, bool status)
{
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    if(status != radiobtn->status) {
        radiobtn->status = status;
        sgl_obj_add_dirty_list(obj);
    }
}

bool sgl_radiobtn_get_status(sgl_obj_t *obj)
{
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    return radiobtn->status;
}

void sgl_radiobtn_click(sgl_obj_t *obj)
{
    sgl_obj_set_event_status(obj, SGL_EVENT_PRESSED);
    sgl_obj_add_dirty_list(obj);
}

void sgl_radiobtn_set_font(sgl_obj_t *obj, sgl_font_t *font)
{
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    radiobtn->font = font;
}

void sgl_radiobtn_set_text(sgl_obj_t *obj, const char* text)
{
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    radiobtn->text = text;
}

void sgl_radiobtn_draw(sgl_obj_t *obj)
{
    int r = obj->size.h / 2;
    int weight = obj->size.h / 6;
    int pos_x, pos_y;
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    sgl_widget_draw_rect(rect);
    sgl_surf_t *surf = sgl_draw_get_active_surf(obj);
    sgl_widget_buffer_valid(obj);
    sgl_draw_buffer_clear(surf, obj->parent->style.body_color);
    pos_x = sgl_widget_draw_ofs_x(r);
    pos_y = sgl_widget_draw_ofs_y(r);
    sgl_draw_circle_solid(surf, pos_x, pos_y, r, obj->style.body_color, obj->parent->style.body_color);
    sgl_draw_circle_solid(surf, pos_x, pos_y, r - weight, obj->parent->style.body_color, obj->style.body_color);
    if(radiobtn->status) {
        sgl_draw_circle_solid(surf, pos_x, pos_y, r - weight * 2, obj->style.body_color, obj->parent->style.body_color);
    }
    if(radiobtn->text && radiobtn->font) {
        pos_x = sgl_widget_draw_ofs_x(obj->size.h + 2);
        pos_y = sgl_widget_draw_ofs_y((obj->size.h - sgl_text_height(radiobtn->font))/2);
        sgl_draw_font_string(surf, radiobtn->text, pos_x, pos_y, obj->style.text_color, obj->parent->style.body_color, radiobtn->font); 
    }
    sgl_draw_obj_selected(obj, surf, rect);
    sgl_widget_draw_buffer_flush;
}


void sgl_radiobtn_event_cb(sgl_obj_t *obj)
{
    sgl_radiobtn_t *radiobtn = container_of(obj, sgl_radiobtn_t, obj);
    if(obj->ev_stat == SGL_EVENT_PRESSED) {
        radiobtn->status = !radiobtn->status;
        sgl_obj_event_cb(obj);
    }
    sgl_radiobtn_draw(obj);
}
